let app = getApp();

var wxCharts = require('../../utils/wxcharts.js');
var ringChart = null;

   

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLoading: true,
        libraryTab:1,
        onshow:0,
        attractList:[],
        count:0,
        enterprise:[],
        browseCount:0,
        investType:[],
        istest:0,
        keyword:'',
        projectList:[]
    },

    //创建圆环
    createRing:function(canvasId,series,count){
        new wxCharts({
            canvasId: canvasId,
            type: 'ring',
            position:'top',
            series:series,
            width: 300,
            height: 250,
            dataLabel: true,
            legend: true,
            extra: {
            ringWidth:10,//圆环的宽度
                pie: {
                    offsetAngle: -45//圆环的角度
                }
            },
            disablePieStroke: true,
            padding: 0,
            title: {
            name: count,
            color: '#7cb5ec',
            fontSize:18
            },
            subtitle: {
            name: '总项目',
            color: '#666666',
            fontSize: 16
            },
            cultivation:[], //培育类
            condition:[], //进规进限类
            
        });
    },

    onClickDetail:function(e){
        var libraryTab = this.data.libraryTab;
        wx.navigateTo({ url: `/pages/project/project?id=${e.currentTarget.dataset.id}&libraryTab=${libraryTab}` })
    },

    searachRemove:function(e){
        this.setData({keyword:'',projectList:[]});
    },
    searachApi:function(e){
        var keyword = e.detail.value;
        this.setData({keyword:keyword});
        var library = this.data.libraryTab;
        var keyword = this.data.keyword;

        if(keyword!=''){
            var params = {library:library,keyword:keyword};
            //获取项目列表
            app.request('post', '/api/Project/listApi', params).then(res => {
               let projectList = res.data.list;
               this.setData({projectList:projectList});
           }).catch(err => console.log(err));
        }else{
            this.setData({keyword:'',projectList:[]});
        }
    },

    onReachBottom() {
        console.log('上拉触底事件触发')
        this.setData({istest:"上拉触底事件触发"});
    },

    onMenuTab:function(e){
        this.setData({keyword:'',projectList:[]});
        this.setData({libraryTab:e.currentTarget.dataset.by,onshow:0});
        if(e.currentTarget.dataset.by==1){
            this.twinkle();
        }
        if(e.currentTarget.dataset.by==2){
            this.twinkle();
        }

        if(e.currentTarget.dataset.by==4){
            this.twinkle();
        }

        if(e.currentTarget.dataset.by==3){
            this.twinkle();
        }

        if(e.currentTarget.dataset.by==6){
            this.twinkle();
        }

        if(this.data.libraryTab!=6){
            this.statisticsApi();
        }else{
            this.statisticsList();
        }
    },

    
    statisticsList:function(){
        var self = this;
        var library = this.data.libraryTab;
        var params = {library:library}
        //获取统计数据  
        app.request('post', '/api/Project/statisticsApi', params).then(res => {
            /*var series= res.data.data;
            var count = res.data.count;
            var browseCount = res.data.browseCount;
            self.setData({enterprise:series,count:count,browseCount:browseCount});*/
            var count = res.data.count;
            var cultivation= res.data.cultivation; //培育类
            var condition= res.data.condition; //进规进限类
            var browseCount = res.data.browseCount;
            self.setData({cultivation:cultivation,count:count,browseCount:browseCount,condition:condition});

        }).catch(err => console.log(err));
    },

    statisticsApi:function(){
        var self = this;
        var library = this.data.libraryTab;
        var params = {library:library}
        //获取统计数据  
        app.request('post', '/api/Project/statisticsApi', params).then(res => {
            var series= res.data.series;
            var count = res.data.count;
            var browseCount = res.data.browseCount;
            var investType= res.data.investType;
            var attractList= res.data.attractList;
            
            self.setData({attractList:attractList,count:count,browseCount:browseCount,investType:investType});
            self.createRing("ringCanvas",series,count)
        }).catch(err => console.log(err));
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
       
        this.twinkle();
        this.setData({
            imageWidth: wx.getSystemInfoSync().windowWidth
          })
    },


    twinkle:function(){
        var s = this.data.onshow;//设置轮询次数
        var _time;
        _time = setInterval(() => {
            //此处写自己的业务逻辑。。。。。。
            s ++;
            this.setData({
                onshow:s,
            });
            if(s > 6){
                //最大轮询30次，及90秒后，关闭轮询
                clearInterval(_time);
            }
    
        }, 500);//3000:每3秒轮询一次
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(options.libraryTab!=undefined){
            this.setData({libraryTab:options.libraryTab});
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.popup = this.selectComponent('#popup');
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    //取消事件
    _error() {
        this.popup.hidePopup();
    },
    

    touchHandler: function (e) {
        console.log(ringChart.getCurrentDataIndex(e));
    },
    updateData: function () {
        ringChart.updateData({
            title: {
                name: '80%'
            },
            subtitle: {
                color: '#ff0000'
            }
        });
    },     

    

    onReady: function (e) {
        // format:function(){
        //     return "44个";
        // }
        if(this.data.libraryTab!=6){
            this.statisticsApi();
        }else{
            this.statisticsList();
        }
    },

    //招商引资（在谈项目入口）
    groupLink:function(e){
      var sign = e.currentTarget.dataset.val;
      wx.navigateTo({
        url: '/pages/group/group?libraryTab='+this.data.libraryTab+'&sign='+sign
      })
    },

    //跳转招商列表
    signList:function(e){
        var sign = e.currentTarget.dataset.val;
        wx.navigateTo({
            url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&sign='+sign
        })
    },

    //跳转预备库列表
    prepareList:function(e){
        var start_status = e.currentTarget.dataset.val;
        // wx.navigateTo({
        //     url: '/pages/itemizeApi/itemizeApi?libraryTab='+this.data.libraryTab+'&start_status='+start_status
        // })
        //待开工项目跳转分组
        if(start_status==1){
          wx.navigateTo({
            url: '/pages/group/type?libraryTab='+this.data.libraryTab+'&start_status='+start_status
        })
        }else{
          wx.navigateTo({
            url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&start_status='+start_status
        })
        }
    },

    //统计未入库
    warehousingList:function(e){
        wx.navigateTo({
            url: '/pages/showList/showList?libraryTab=3&warehousing=1'
        })
        // wx.navigateTo({
        //     url: '/pages/itemizeApi/itemizeApi?libraryTab='+this.data.libraryTab+'&warehousing=1'
        // })
    },

    //跳转开工库列表
    startList:function(e){
        var project_status = e.currentTarget.dataset.val+1;
        // wx.navigateTo({
        //     url: '/pages/itemizeApi/itemizeApi?libraryTab='+this.data.libraryTab+'&project_status='+project_status
        // })
      if(project_status==1 | project_status==3){
          wx.navigateTo({
                url: '/pages/group/type?libraryTab='+this.data.libraryTab+'&project_status='+project_status
            })
      }else{
        wx.navigateTo({
          url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&statusCode='+project_status
        })
      }
    },

    //跳转重点项目列表
    stressList:function(e){
        var project_status = e.currentTarget.dataset.val+1;
        wx.navigateTo({
            url: '/pages/akey/akey?libraryTab='+this.data.libraryTab+'&project_status='+project_status
        })
        /*var statusCode = e.currentTarget.dataset.val;
        if(statusCode==5){
            wx.navigateTo({
                url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&projectMyname=1'
            })
        }else{
            wx.navigateTo({
                url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&statusCode='+statusCode
            })
        }*/
        
    },

    //跳转入统库列表
    Integration:function(e){
        var reply = e.currentTarget.dataset.val;
        wx.navigateTo({
            url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&reply='+reply
        })
    },

    //跳转进规进限类界面
    secondView:function(e){
        var condition_val = e.currentTarget.dataset.val;
        wx.navigateTo({
            url: '/pages/secondView/secondView?libraryTab='+this.data.libraryTab+'&condition_val='+condition_val
        })
    },


    //跳转培育类页面
    newListTap:function(e){
        wx.navigateTo({
          url: '/pages/showList/showList?libraryTab=6&enterprise='+e.currentTarget.dataset.id+'&menuTop='+e.currentTarget.dataset.menutop
        })

        // wx.navigateTo({
        //     url: '/pages/newList/newList?enterprise='+e.currentTarget.dataset.id+"&menuTop="+e.currentTarget.dataset.menutop
        // })
    },

    //跳转访客列表
    browse:function(){
        var libraryTab = this.data.libraryTab;
        wx.navigateTo({
            url: '/pages/browse/browse?libraryTab='+libraryTab
        })
    },

    //跳转项目列表
    showListApi:function(e){
        wx.navigateTo({
            url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&invest_type='+e.currentTarget.dataset.val
        })
    },

    //跳转我的项目
    myList:function(e){
      wx.navigateTo({
          url: '/pages/showList/showList?libraryTab='+this.data.libraryTab+'&projectMyname=1'
      })
  },

})